<?php

class Part extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'parts':
	 * @var integer $id
	 * @var integer $lft
	 * @var integer $rgt
	 * @var integer $parent_id
	 * @var integer $level
	 * @var string $title
	 * @var string $alias
	 * @var string $url
	 */
	 
	var $_active = false;
	
	public function behaviors(){
		return array(
			'TreeBehavior' => array(
				'class' => 'ext.nestedset.TreeBehavior'
			)
		);
	}

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'parts';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title', 'required'),
			array('lft, rgt, parent_id, level', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>80),
			array('alias', 'length', 'max'=>45),
			array('alias', 'unique', 'allowEmpty'=>true),
			array('url', 'length', 'max'=>255),
			array('page_title, page_description, page_keywords', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, lft, rgt, parent_id, level, title, alias, url', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'pages' => array(self::HAS_MANY, 'Page', 'part_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'lft' => 'Lft',
			'rgt' => 'Rgt',
			'parent_id' => 'Parent',
			'level' => 'Level',
			'title' => 'Название',
			'alias' => 'Псевдоним',
			'url' => 'Ссылка',
			'page_title' => 'Тайтл',
			'page_description' => 'Описание',
			'page_keywords' => 'Ключевые слова',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('lft',$this->lft);

		$criteria->compare('rgt',$this->rgt);

		$criteria->compare('parent_id',$this->parent_id);

		$criteria->compare('level',$this->level);

		$criteria->compare('title',$this->title,true);

		$criteria->compare('alias',$this->alias,true);

		$criteria->compare('url',$this->url,true);

		return new CActiveDataProvider('Part', array(
			'criteria'=>$criteria,
		));
	}
	
	static public function getByAlias($alias, $route = false){
		$part = self::model()->find('alias=:alias', array(':alias'=>$alias));
		if($route){
			if($part->url) {
				$a = eval('return array('.$part->url.');');
				$url = trim($a[0],'/');
			} else
				return $part;

			if($alias == 'home' && $route == 'site/index')
				$part->_active = true;
			else {
				$flag_strlen = 0;
				$i = 0;
				do{
					$pos=strrpos($route, '/');

					if(strcmp($url,$route) != 0) {
						$route = substr($route, 0, $pos);
					} elseif($flag_strlen < strlen($route)) {
						$part->_active = true;
						$flag_strlen = strlen($route);
						$pos = false;
					}

					$i++;
					//echo '<br />';
				}while($pos && $i < 100);
			}
		}
		
		return $part;
	}
}